package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


public class SearchPanel4 extends JPanel{
	private static final long serialVersionUID = -4978355936810558444L;
	JLabel searchLabel;
	JButton searchButton, deleteButton;
	JTextField searchTextField;
	JTable table;
	JScrollPane scrollPane;
	Vector<Vector<Object>> data;
	
	private GUI guiObject;
	private SellPage callingPanel;
	
	public void clearFields()
	{
		searchTextField.setText("");
		data.clear();
		table.updateUI();
	}

	public SearchPanel4(GUI guiObject, SellPage callingPanel)
	{
		this.guiObject = guiObject;
		this.callingPanel = callingPanel;
		setLayout(new GridBagLayout());
		createSearchPanel();
		createTable();
		setMinimumSize(new Dimension(700, 400));
	}
	
	private void createSearchPanel()
	{
		searchLabel = new JLabel("Books You Are Selling: Search By Name");
		searchTextField = new JTextField();
		searchButton = new JButton("Search");
		
		searchButton.setActionCommand("sellsearch");
		searchButton.addActionListener(guiObject);
		
		searchTextField.setPreferredSize(new Dimension(500, 20));
		
		Constraints.setConstraints(0,0,2,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
		add(new JSeparator(SwingConstants.HORIZONTAL), Constraints.gbc);
		
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
		add(searchLabel, Constraints.gbc);
		
		Constraints.setConstraints(0,2,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
		add(searchTextField, Constraints.gbc);
		
		Constraints.setConstraints(1,2,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
		add(searchButton, Constraints.gbc);
	}
	
	private void createTable()
	{
        Vector<String> columnNames = new Vector<String>();
        columnNames.add("Book Name");
        columnNames.add("Author");
        columnNames.add("Edition");
        columnNames.add("Quality");
        columnNames.add("ISBN");
        columnNames.add("Price");

        data = new Vector<Vector<Object>>();
        
//        Vector<Object> rowHolder = new Vector<Object>();
        
        data.addAll(guiObject.dL.getBookList(""));

        table = new JTable(data, columnNames) 
        {
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
              }
        };
        table.setPreferredScrollableViewportSize(new Dimension(600, 475));

        table.setFillsViewportHeight(true);
        table.setShowGrid(false);
        
        table.addMouseListener(new MouseAdapter() {
        	public void mouseClicked(MouseEvent e) {
        		int row = table.getSelectedRow();
        		callingPanel.bookNameField.setText((String)data.get(row).get(0));
        		callingPanel.authorField.setText((String)data.get(row).get(1));
        		callingPanel.editionField.setText((String)data.get(row).get(2));
        		callingPanel.qualityField.setText((String)data.get(row).get(3));
        		callingPanel.ISBNField.setText((String)data.get(row).get(4));
        		callingPanel.priceField.setText(""+(Double)data.get(row).get(5));
        	}
        });

        //Create the scroll pane and add the table to it.
        scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        
        Constraints.setConstraints(0,3,2,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
        
        add(scrollPane, Constraints.gbc);
		
		deleteButton = new JButton("Delete Book");
		deleteButton.setActionCommand("selldelete");
		deleteButton.addActionListener(guiObject);
        
		Constraints.setConstraints(1,1,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
				
		add(deleteButton, Constraints.gbc);
	}
}